package com.system.business.service.impl;

import com.system.business.been.Business;
import com.system.business.been.IntervalPlanLst;
import com.system.business.repository.BusinessRepository;
import com.system.business.service.BusinessService;
import com.system.business.service.IntervalPlanLstService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 林
 */
@Service
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private BusinessRepository businessRepository;
    @Autowired
    private IntervalPlanLstService intervalPlanLstService;
    @Override
    public boolean saveBusiness(Business business) {
        Business obj = businessRepository.save(business);
        if(obj != null){
            return true;
        }
        return false;
    }

    @Override
    public boolean removeBusinessById(int id) {
        if (businessRepository.existsById(id)) {
            businessRepository.deleteById(id);
            return true;
        }
        return false;
    }

    @Override
    public boolean modifyBusiness(Business business) {
        Business obj = businessRepository.save(business);
        if(obj != null){
            return true;
        }
        return false;
    }

    @Override
    public Business getBusinessById(int id) {
        if (businessRepository.existsById(id)) {
            return businessRepository.getReferenceById(id);
        }
        return null;
    }

    @Override
    public List<Business> listAllBusinesses() {
        return businessRepository.findAll();
    }

    @Override
    public int findByBusinessId(int businessId) {
        Business business = getBusinessById(businessId);
        return business.getIntervalPlanId();
    }

    @Override
    public List<IntervalPlanLst> findAllIntervalPlanLstByBusinessId(int businessId) {
        int intervalPlanId = findByBusinessId(businessId);
        return intervalPlanLstService.findAllByIntervalPlanId(intervalPlanId);
    }

    @Override
    public List<Business> findAllByDepartmentId(int departmentId) {
        return businessRepository.findAllByDepartmentId(departmentId);
    }
}
